﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.DataAccess.Store {
    public class OutsourceQuotationRepository : BaseRepository<OutsourceQuotation> {

        private int _pageSize;

        public OutsourceQuotationRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public OutsourceQuotation FetchOutsourceQuotationById(int outsourceQuotationId) {
            using (var ctx = this.Context) {
                return ctx.OutsourceQuotations.Find(outsourceQuotationId);
            }
        }

        public List<OutsourceQuotation> FetchOutsourceQuotationsByVendorId(int vendorId) {
            using (var ctx = this.Context) {
                return ctx.OutsourceQuotations
                    .Include("Product")
                    .Include("Product.Basic")
                    .Where(x => x.VendorId == vendorId).ToList();
            }
        }

        public void DeleteMultipleOutsourceQuotation(params int[] outsourceQuotationIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.OutsourceQuotations.Where(x => outsourceQuotationIds.Contains(x.OutsourceQuotationId)).ToList().ForEach(x => {
                            ctx.OutsourceQuotations.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public List<OutsourceQuotation> FetchOutsourceQuotationList() {
            using (var ctx = this.Context) {
                return ctx.OutsourceQuotations.ToList();
            }
        }

        public List<OutsourceQuotation> FetchOutsourceQuotationList(OutsourceQuotationSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.OutsourceQuotations
                    .Include("Attachment")
                    .Include("Basic")
                    .Include("Product")
                    .Include("Vendor")
                    .Where(x => x.OutsourceQuotationId != 0);

                if (searchModel.VendorId.HasValue) {
                    query = query.Where(x => x.VendorId == searchModel.VendorId.Value);
                }
                if (searchModel.ProductId.HasValue) {
                    query = query.Where(x => x.ProductId == searchModel.ProductId.Value);
                }
                if (searchModel.PriceStart.HasValue) {
                    query = query.Where(x => x.Price >= searchModel.PriceStart.Value);
                }
                if (searchModel.PriceEnd.HasValue) {
                    query = query.Where(x => x.Price <= searchModel.PriceEnd.Value);
                }

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderByDescending(x => x.CreatedDatetime).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }

    }
}
